package com.xc.freeapp.account.controller;

import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import com.github.miemiedev.mybatis.paginator.domain.PageList;
import com.github.miemiedev.mybatis.paginator.domain.Paginator;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.xc.freeapp.bean.AuthInfo;
import com.xc.freeapp.common.CheckUtils;
import com.xc.freeapp.common.ResponseBean;
import com.xc.freeapp.common.ResponsePage;
import com.xc.freeapp.entity.Bookingreg;
import com.xc.freeapp.entity.Messages;
import com.xc.freeapp.entity.Regpatient;
import com.xc.freeapp.exception.BaseException;
import com.xc.freeapp.request.bean.RelationAndIssue;
import com.xc.freeapp.service.RegpatientService;
import com.xc.freeapp.util.AuthUtil;
import com.xc.freeapp.util.CertNoUtil;
import com.xc.freeapp.util.Constant;
import com.xc.freeapp.util.DateUtils;
import com.xc.freeapp.util.TokenAnnotation;

@Controller
@RequestMapping("/regpatient")
@Api(value = "regpatient", description = "就诊人")
public class RegpatientController extends AbstractController {
	@Autowired
	RegpatientService regpatientService;

	@TokenAnnotation(required = true)
	@RequestMapping(value = "", method = RequestMethod.POST)
	@ResponseBody
	@ApiOperation(value = "保存就诊人")
	public Regpatient save(@RequestBody @Valid Regpatient regpatient)
			throws BaseException, Exception {
		AuthInfo authInfo = AuthUtil.getAuthInfo(getRequest());
		if (regpatient.getPatientdentityno() != null
				&& regpatient.getPatientdentityno().length() == 15) {

		} else if (!CertNoUtil.vId(regpatient.getPatientdentityno())) {
			throw new BaseException(Constant.ERRORCODE_556);
		}

		if (regpatient.getTelphone() != null
				&& !"".equals(regpatient.getTelphone())) {
			if (regpatient.getTelphone().length() != 11) {
				throw new BaseException(Constant.ERRORCODE_115);
			}

		}

		PageBounds pageBounds = new PageBounds(1, 1000);

		regpatient.setCreateby(authInfo.getUserIntId());
		regpatient.setCreatetime(DateUtils.getDate());
		regpatient.setUpdatetime(DateUtils.getDate());
		regpatient.setDelflag("t");
		regpatient.setUpdateby(authInfo.getUserIntId());
		
		if ("t".equals(regpatient.getIsdefault())) {
			List<Regpatient> regpatients = regpatientService.selectByUserId(
					authInfo.getUserIntId(), pageBounds);
			for (Regpatient regpatient2 : regpatients) {
				regpatient2.setIsdefault("f");
				regpatientService.updateByPrimaryKey(regpatient2);
			}
		}
		regpatientService.insert(regpatient);
		return regpatient;
	}

	@TokenAnnotation(required = true)
	@RequestMapping(value = "", method = RequestMethod.PUT)
	@ResponseBody
	@ApiOperation(value = "修改就诊人")
	public Regpatient update(@RequestBody Regpatient regpatient)
			throws BaseException, Exception {
		AuthInfo authInfo = AuthUtil.getAuthInfo(getRequest());
		if (regpatient.getPatientdentityno() != null
				&& regpatient.getPatientdentityno().length() == 15) {

		} else if (!CertNoUtil.vId(regpatient.getPatientdentityno())) {
			throw new BaseException(Constant.ERRORCODE_556);
		}

		if (regpatient.getTelphone() != null
				&& !"".equals(regpatient.getTelphone())) {
			if (regpatient.getTelphone().length() != 11) {
				throw new BaseException(Constant.ERRORCODE_115);
			}

		}

		PageBounds pageBounds = new PageBounds(1, 1000);
		List<Regpatient> regpatients = regpatientService.selectByUserId(
				authInfo.getUserIntId(), pageBounds);
		regpatient.setCreateby(authInfo.getUserIntId());
		regpatient.setUpdateby(authInfo.getUserIntId());
		regpatient.setUpdatetime(DateUtils.getDate());
		regpatientService.updateByPrimaryKey(regpatient);
		if ("t".equals(regpatient.getIsdefault())) {
			for (Regpatient regpatient2 : regpatients) {
				if (!regpatient2.getId().equals(regpatient.getId())) {
					regpatient2.setIsdefault("f");
					regpatientService.updateByPrimaryKey(regpatient2);
				}

			}
		}
		return regpatient;
	}

	@TokenAnnotation(required = true)
	@RequestMapping(value = "setisdefault", method = RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value = "设置默认就诊人")
	public ResponseBean setisdefault(@RequestParam Integer id) throws BaseException,
			Exception {
		AuthInfo authInfo = AuthUtil.getAuthInfo(getRequest());
		PageBounds pageBounds = new PageBounds(1, 1000);
		List<Regpatient> regpatients = regpatientService.selectByUserId(
				authInfo.getUserIntId(), pageBounds);
		for (Regpatient regpatient2 : regpatients) {
			if (regpatient2.getId().equals(id)) {
				regpatient2.setIsdefault("t");
			} else {
				regpatient2.setIsdefault("f");
			}

			regpatientService.updateByPrimaryKey(regpatient2);
		}
		return ResponseBean.getSuccess();
	}

	@TokenAnnotation(required = true)
	@RequestMapping(value = "", method = RequestMethod.GET)
	@ResponseBody
	@ApiOperation(value = "查询就诊人")
	public ResponsePage<List<Regpatient>> select(
			@RequestParam(value = "page", required = false, defaultValue = "0") Integer page,
			@RequestParam(value = "limit", required = false, defaultValue = "15") Integer limit)
			throws BaseException, Exception {
		AuthInfo authInfo = AuthUtil.getAuthInfo(getRequest());
		PageBounds pageBounds = new PageBounds(page, limit);
		List<Regpatient> regpatients = regpatientService.selectByUserId(
				authInfo.getUserIntId(), pageBounds);
		Paginator paginator = ((PageList) regpatients).getPaginator();
		return new ResponsePage<List<Regpatient>>(regpatients, paginator);
	}

	@TokenAnnotation(required = true)
	@RequestMapping(value = "", method = RequestMethod.DELETE)
	@ResponseBody
	@ApiOperation(value = "删除就诊人")
	public ResponseBean select(@RequestParam Integer id) throws BaseException,
			Exception {
		AuthInfo authInfo = AuthUtil.getAuthInfo(getRequest());
		log.info("id=" + id);
		regpatientService.deleteByPrimaryKey(id);
		return ResponseBean.getSuccess();
	}

}
